<?php

if (!defined('BASEPATH'))
    exit('No diresct script access not allowed');

class Specification_model extends CI_Model {

    function _construct() {
        parent::__construct();
        log_message('debug', 'specification model initialized');
    }

    //-----Insert specifications group For Specification management-----
    function specification_group_insert($type=NULL, $specification_group=NULL, $code=NULL) {
        $sql = "insert into specification(type,code,slno,specification) values('$type','$code','0','$specification_group')";
        $query = $this->db->query($sql);
    }

    //-----Edit specifications group For Specification management-----
    function specification_group_modify($type=NULL, $specification_group=NULL, $code=NULL) {
        $sql = "update specification set specification='$specification_group' where code='$code' and slno=0";
        $query = $this->db->query($sql);
    }

    //-----Delete specifications group For Specification management-----
    function specification_group_delete($type=NULL, $code=NULL) {
        $sql = "delete from specification where type='$type' and code='$code'";
        $query = $this->db->query($sql);
    }

    //-----Insert specifications For Specification management-----
    function specification_insert($type=NULL, $specification=NULL, $code=NULL, $slno=NULL) {
        $sql = "insert into specification(type,code,slno,specification) values('$type','$code','$slno','$specification')";
        $query = $this->db->query($sql);
    }

    //-----Edit specifications For Specification management-----
    function specification_modify($type=NULL, $specification_group=NULL, $id=NULL) {
        $sql = "update specification set specification='$specification_group' where id=$id";
        $query = $this->db->query($sql);
    }

    //-----Delete specification For Specification management-----
    function specification_delete($type=NULL, $id=NULL) {
        $sql = "delete from specification where type='$type' and id='$id'";
        $query = $this->db->query($sql);
    }

    //----------All row return For Specification management-----
    function get_specifications_groups($type=NULL, $order_type=NULL, $num=NULL, $offset=NULL) {
        $subcond = " where slno='0' ";
        $limit = '';
        if ($type != NULL)
            $subcond.=" and type='$type'";
        if ($num > 0)
            $limit = ' limit ' . $offset . ',' . $num;
        $sql = "select * from specification " . $subcond . ' order by specification ' . $order_type . $limit;
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

    //----------Single row return For Specification management-----
    function get_specifications_group($type=NULL) {
        
    }

    //----------Multi ROw returen where slno not 0 all specifications

    //----------get specifications all

    function get_specifications($type=NULL, $code=NULL, $order_type=NULL, $num=NULL, $offset=NULL) {
        $subcond = ' where slno<>0 ';

        if ($type != NULL)
            $subcond.=" and type='$type'";
        if ($code != NULL && is_numeric($code) && $code > 0)
            $subcond.=" and code=$code";
        if ($num > 0)
            $limit = ' limit ' . $offset . ',' . $num;
        $sql = "select * from specification " . $subcond . ' order by specification ' . $order_type . $limit;
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }


 
    function get_specification($type='CAR', $version=0,$slno=0) {
        $sql="SELECT specification_value FROM `specification_master` WHERE type='$type' and version=$version and slno=$slno";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $row = $query->row();
            return $row->specification_value;
        }
        return FALSE;
    }
    
    //----------Get one Specification row
    function get_specification_master($type='CAR',$version=0)
    {
        $sql = "select * from specification_master where type='$type' and version=$version";   
        $query=$this->db->query($sql);
        $result=$query->result();
        if($result){
            return $result;
        }else{
            return false;
        }
        
    }

    //--- To fatch highest slno from specification 
    function get_max_slno($code=NULL) {
        $sql = "select max(slno) as no from specification where code='$code'";
        $query = $this->db->query($sql);
        $row = $query->row();
        return intval($row->no) + 1;
    }
    //--- To fatch highest code no from specification --
    function get_max_code() {
        $sql = "select max(code) as maxno from specification where code<>0 and slno = 0";
        $query = $this->db->query($sql);
        $row = $query->row();
        return intval($row->maxno) + 1;
    }
    

}
?>